
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import com.jme.bounding.BoundingBox;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.state.ZBufferState;
import com.jme.scene.state.ZBufferState.TestFunction;
import com.jme.system.DisplaySystem;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;
/**
 * @author sharavsambuu
 * */
public class Util {

    // So JME can find the model textures easily.
    private static URL resourceSearchPath;

    static {
        resourceSearchPath = Util.class.getResource( "data/hummer_jeep/" );
        try {
            ResourceLocatorTool.addResourceLocator( ResourceLocatorTool.TYPE_TEXTURE, new SimpleResourceLocator( resourceSearchPath ) );
        } catch ( URISyntaxException e1 ) {
            e1.printStackTrace();
        }
    }

    /**
     * Loads a jme binary model from file, creating a bounding box for it.
     *
     * @param path to jme file
     * @return loaded model, null on error
     */
    public static Node loadModel( String path ) {
        try {
            URL modelURL = new URL( resourceSearchPath, path );
            BufferedInputStream stream = new BufferedInputStream( modelURL.openStream() );
            Node node = new Node();
            node.attachChild( (Spatial) BinaryImporter.getInstance().load( stream ) );
            node.setModelBound( new BoundingBox() );
            node.updateModelBound();
            return node;
        } catch ( IOException e ) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static Node loadObjModel() {
    	Spatial spatial;
    	URL folder= Util.class.getClassLoader().getResource("data/hummer_jeep/");	
		URL model = Util.class.getClassLoader().getResource("data/hummer_jeep/hummer_jeep.obj");
        FormatConverter converter=new ObjToJme();
        converter.setProperty("mtllib", folder);
        converter.setProperty("texdir", folder);
        ByteArrayOutputStream BO=new ByteArrayOutputStream();
        try {
            converter.convert(model.openStream(), BO);
            spatial=(Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));            
            spatial.setModelBound(new BoundingBox());
            spatial.updateModelBound();          
            Node node = new Node ();
            node.attachChild(spatial);
            node.setModelBound(new BoundingBox());
            node.updateModelBound();
            return node;
        } catch (Exception e) {
        	System.err.println("Уучлаарай жийпний модел ачаалах хэсэгт алдаа гарлаа");
            return null;
        }    	
		
    }

    /**
     * Helper method to apply a ZBufferState to a node.
     *
     * @param node where to apply to
     */
    public static void applyZBuffer( Node node ) {
        ZBufferState zbuf = DisplaySystem.getDisplaySystem().getRenderer().createZBufferState();
        zbuf.setWritable( false );
        zbuf.setEnabled( true );
        zbuf.setFunction( TestFunction.LessThanOrEqualTo );
        node.setRenderState(zbuf);
	}
}
